using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 生命值组件
/// </summary>
public class HealthComponent : MonoBehaviour
{
    public long MaxHealth { get; private set; }

    public long CurHealth { get; private set; }

    /// <summary>
    /// 是否无敌
    /// </summary>
    public bool IsInvincible { get; private set; }

    /// <summary>
    /// 是否免疫物理伤害
    /// </summary>
    public bool IsImmuneToPhysical { get; private set; }

    /// <summary>
    /// 是否免疫魔法伤害
    /// </summary>
    public bool IsImmuneToMagic { get; private set; }

    /// <summary>
    /// 是否具有物理护盾
    /// </summary>
    public bool HasPhysicalShield { get; private set; }

    public virtual void TakeDamage(long damage)
    {
        if (IsInvincible)
        {
            Logger.Log("HealthComponent", $"{name} 处于无敌状态，免疫伤害: {damage}");
            return;
        }

        // 检查物理护盾
        if (HasPhysicalShield)
        {
            // 这里需要与PhysicalShieldBuff交互来处理护盾伤害
            // 暂时直接造成伤害，实际应该通过Buff系统处理
        }

        CurHealth = Math.Max(0, CurHealth - damage);
        
        // 触发受伤事件
        var damageArgs = ReferencePool.Acquire<EntityTakeDamageEventArgs>();
        damageArgs.Entity = GetComponent<Entity>();
        damageArgs.Damage = damage;
        damageArgs.Attacker = null;
        EventHelper.Fire(this, damageArgs);
        
        // 检查是否死亡
        if (IsDead)
        {
            var deathArgs = ReferencePool.Acquire<EntityDeathEventArgs>();
            deathArgs.Entity = GetComponent<Entity>();
            deathArgs.Killer = null;
            EventHelper.Fire(this, deathArgs);
        }
    }

    public virtual void TakeDamage(long damage, DamageType damageType)
    {
        if (IsInvincible)
        {
            Logger.Log("HealthComponent", $"{name} 处于无敌状态，免疫伤害: {damage}");
            return;
        }

        // 检查伤害类型免疫
        if (damageType == DamageType.Physical && IsImmuneToPhysical)
        {
            Logger.Log("HealthComponent", $"{name} 免疫物理伤害: {damage}");
            return;
        }

        if (damageType == DamageType.Magic && IsImmuneToMagic)
        {
            Logger.Log("HealthComponent", $"{name} 免疫魔法伤害: {damage}");
            return;
        }

        TakeDamage(damage);
    }

    public virtual void TakeHeal(long heal) => CurHealth = Math.Clamp(CurHealth + heal, 0, MaxHealth);

    public bool IsDead => CurHealth <= 0;

    public void Initialize(long health)
    {
        MaxHealth = health;
        CurHealth = health;
        IsInvincible = false;
        IsImmuneToPhysical = false;
        IsImmuneToMagic = false;
        HasPhysicalShield = false;
    }

    /// <summary>
    /// 设置无敌状态
    /// </summary>
    /// <param name="invincible">是否无敌</param>
    public void SetInvincible(bool invincible)
    {
        IsInvincible = invincible;
    }

    /// <summary>
    /// 设置物理伤害免疫
    /// </summary>
    /// <param name="immune">是否免疫</param>
    public void SetImmuneToPhysical(bool immune)
    {
        IsImmuneToPhysical = immune;
    }

    /// <summary>
    /// 设置魔法伤害免疫
    /// </summary>
    /// <param name="immune">是否免疫</param>
    public void SetImmuneToMagic(bool immune)
    {
        IsImmuneToMagic = immune;
    }

    /// <summary>
    /// 设置物理护盾状态
    /// </summary>
    /// <param name="hasShield">是否有护盾</param>
    public void SetPhysicalShield(bool hasShield)
    {
        HasPhysicalShield = hasShield;
    }
}